/**
 * Copyright (C), 2018 store
 * Encoding: UTF-8
 * Date: 19-12-10 下午4:35
 * History:
 */
package com.swak.frame.excel;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.core.io.Resource;

import com.swak.frame.excel.bo.RowObject;
import com.swak.frame.excel.bo.SheetObject;
import com.swak.frame.excel.exception.ExcelException;
import com.swak.frame.logger.SwakLoggerFactory;
import com.swak.frame.util.GetterUtil;


/**
 * ExcelLoaderFactory.java
 * 
 * @author ColleyMa
 * @version 19-12-10 下午4:35
 */
public class ExcelLoaderFactory extends AbstractLoaderSupportFactory {
    /** Logger available to subclasses */
    protected Logger logger = SwakLoggerFactory.getLogger(getClass()); // 日志记录
    private String notBlankCol;
    private ExcelHelper excelHelper;
    private String colKey;

    public void setNotBlankCol(String notBlankCol) {
        this.notBlankCol = notBlankCol;
    }

    public void setColKey(String colKey) {
        this.colKey = colKey;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.excelHelper = ExcelHelper.getInstance();
        super.afterPropertiesSet();
    }

    @Override
    public void loadProp(Map<String, Map<String, String>> localProp) {
        if (this.locations != null) {
            for (Resource location : this.locations) {
                long startTime = System.currentTimeMillis();
                logger.warn("loadding  excel config file start,path:{} >>>>>>>>",
                    location.getFilename());
                try {
                    String[] notBlankColArr = GetterUtil.getSplitStr(notBlankCol, ",");
                    List<SheetObject> sheet =
                        excelHelper.getInputValuesBySheet(location.getFile(), notBlankColArr);
                    SheetObject sheetObject = sheet.get(0);
                    List<RowObject> rowList = sheetObject.getSheetData();

                    for (int i = 0; i < rowList.size(); i++) {

                        Map<String, String> rowMap = rowList.get(i).getCellValuesMap();
                        if (StringUtils.isEmpty(colKey)) {
                            colKey = sheetObject.getHeader().get(String.valueOf(0));
                        }
                        String keyVal = rowMap.get(colKey);
                        localProp.put(keyVal, rowMap);
                    }
                    logger.warn("loadding excel config file end,path:{},cost:{} ms >>>>>>>>",
                        location.getFilename(), (System.currentTimeMillis() - startTime));
                } catch (IOException ex) {
                    // Resource not found when trying to open it
                    if ((ex instanceof FileNotFoundException
                        || ex instanceof UnknownHostException)) {
                        if (logger.isInfoEnabled()) {
                            logger.info("excel resource not found: " + ex.getMessage());
                        }
                    } else {
                        throw new ExcelException("loadding  excel config file error", ex);
                    }
                }
            }
        }
    }


}
